package com.wkg.springblogdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.wkg.springblogdemo.common.exception.BlogException;
import com.wkg.springblogdemo.constant.Constants;
import com.wkg.springblogdemo.mapper.BlogMapper;
import com.wkg.springblogdemo.pojo.dataobject.BlogInfo;
import com.wkg.springblogdemo.pojo.dataobject.UserInfo;
import com.wkg.springblogdemo.pojo.request.BlogInfoRequest;
import com.wkg.springblogdemo.pojo.request.UpdateBlogRequtst;
import com.wkg.springblogdemo.pojo.response.BlogDetailResponse;
import com.wkg.springblogdemo.pojo.response.BlogInfoResponse;
import com.wkg.springblogdemo.service.BlogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("blogService")
public class BlogServiceImpl implements BlogService {


    @Resource(name = "blogMapper")
    private BlogMapper blogMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.NOT_DELETE);
        List<BlogInfo> blogInfos = blogMapper.selectList(queryWrapper);

        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo -> {
            BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
            BeanUtils.copyProperties(blogInfo, blogInfoResponse);
            return blogInfoResponse;
        }).collect(Collectors.toList());

        return blogInfoResponses;
    }

    @Override
    public BlogDetailResponse getDetail(Integer blogId) {
        BlogInfo blogInfo = blogMapper.selectById(blogId);
        BlogDetailResponse blogDetailResponse = new BlogDetailResponse();
        BeanUtils.copyProperties(blogInfo, blogDetailResponse);
        return blogDetailResponse;
    }

    @Override
    public Boolean addBlog(BlogInfoRequest blogInfoRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(blogInfoRequest, blogInfo);
        try {
            Integer insert = insertBlog(blogInfo);
            return insert == 1;
        } catch (Exception e) {
            log.info("内容插入失败,e:", e);
        }
        return false;
    }

    public Integer insertBlog(BlogInfo blogInfo) {
        return blogMapper.insert(blogInfo);
    }

    @Override
    public Boolean blogUpdate(UpdateBlogRequtst updateBlogRequtst) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(updateBlogRequtst, blogInfo);
        try {
            Integer result = this.blogUpdateBlog(blogInfo);
            return result == 1;
        } catch (Exception e) {
            log.error("更新数据失败 e:", e);
        }

        return false;
    }


    public Integer blogUpdateBlog(BlogInfo blogInfo) {
        return blogMapper.updateById(blogInfo);

    }


    @Override
    public Boolean deleteUpdate(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.IS_DELETE);
        try {
            Integer result = blogUpdateBlog(blogInfo);
            return result == 1;
        } catch (Exception e) {
            log.error("删除数据失败 e:", e);
        }

        return false;
    }


}
